package dao

import (
	"go_web/test_demo/demo6_db/model"
	"go_web/test_demo/demo6_db/utils"
)

// 根据用户名和密码查询一条数据
func CheckUserNameAndPassword(username, password string) (*model.User, error) {
	sqlStr := "select * from user where username = ? and password = ?"
	row := utils.Db.QueryRow(sqlStr, username, password)
	user := &model.User{}
	row.Scan(&user.ID, &user.Username, &user.Password, &user.Email)
	return user, nil
}

// 根据用户名查询一条数据
func CheckUserName(username string) (*model.User, error) {
	sqlStr := "select * from user where username = ? "
	row := utils.Db.QueryRow(sqlStr, username)
	user := &model.User{}
	row.Scan(&user.ID, &user.Username, &user.Password, &user.Email)
	return user, nil
}

// 插入信息
func SaveUser(username, password, email string) error {
	sqlStr := "insert into user(username,password,email) values(?,?,?)"
	_, err := utils.Db.Exec(sqlStr, username, password, email)
	if err != nil {
		return err
	}
	return nil
}
